package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebAreaLeaderDao;
import ysq.xyg.web.model.po.WebAreaLeader;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebAreaLeaderVo;
import ysq.xyg.web.service.WebAreaLeaderService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 网站模块-区域负责人service实现
 *
 * @author shengQiang yu
 * @date 2019-9-6 15:21:58
 */
@Service("webAreaLeaderService")
public class WebAreaLeaderServiceImpl extends BaseServiceImpl<WebAreaLeader> implements WebAreaLeaderService {

    @Resource
    private WebAreaLeaderDao webAreaLeaderDao;

    private WebAreaLeader webAreaLeader;

    @Autowired
    public void setBaseDao(WebAreaLeaderDao webAreaLeaderDao) {
        this.baseDao = webAreaLeaderDao;
    }


    /**
     * 分页查询
     *
     * @param webAreaLeaderVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-6 15:21:58
     */
    public ReturnEntity page(WebAreaLeaderVo webAreaLeaderVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<WebAreaLeader> list = acceptList(webAreaLeaderVo);
        PageInfo<WebAreaLeader> page=new PageInfo<WebAreaLeader>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webAreaLeaderVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-6 15:21:58
     */
    public ReturnEntity addOrUpdate(WebAreaLeaderVo webAreaLeaderVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webAreaLeaderVo.getId())){
            webAreaLeader=new WebAreaLeader(currentUser);
            isUpate=false;
        }else{
            webAreaLeader=selectByPrimaryKey(webAreaLeaderVo.getId());
            if(ObjectUtil.isNull(webAreaLeader)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webAreaLeader.setUpdateTime(new Date());
            webAreaLeader.setUpdateUser(currentUser.getId());
        }
        webAreaLeader.setName(webAreaLeaderVo.getName());
        webAreaLeader.setPhone(webAreaLeaderVo.getPhone());
        webAreaLeader.setArea(webAreaLeaderVo.getArea());
        webAreaLeader.setSort(webAreaLeaderVo.getSort());
        webAreaLeader.setShowType(webAreaLeaderVo.getShowType());
        webAreaLeader.setUserId(webAreaLeaderVo.getUserId());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webAreaLeader);
        }else{
            num=insert(webAreaLeader);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-6 15:21:58
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebAreaLeader webAreaLeader=selectByPrimaryKey(id);
        if(webAreaLeader!=null){
            webAreaLeader.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webAreaLeader.setUpdateTime(new Date());
            webAreaLeader.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webAreaLeader);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webAreaLeaderVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-9-6 15:21:58
     */
    public List<WebAreaLeader> acceptList(WebAreaLeaderVo webAreaLeaderVo){
        Example example = new Example(WebAreaLeader.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webAreaLeaderVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webAreaLeaderVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return webAreaLeaderDao.selectByExample(example);
    }
}
